cmake_minimum_required(VERSION 3.21.0)
project(toy)

set(CMAKE_CXX_STANDARD 17)

add_executable(toy src/main.cpp src/Person.cpp src/Family.cpp)

add_executable(main2 src/main2.cpp)

add_executable(ActiveOffice src/ActiveOffice.cpp)

add_executable(AboutShardPoint src/AboutShardPoint.cpp)

 

add_executable(ReflectionTest3 src/ReflectionTesty.cpp)



add_executable(AboutTime src/AboutTime.cpp src/TimeUtil.cc)

add_executable(MergeList src/MergeList.cpp)

add_executable(AboutPara src/AboutPara.cpp src/Person.cpp)



add_executable(AboutErase src/AboutErase.cpp )

add_executable(LFUCache src/LFUCache.cpp )


add_executable(BoostSplit src/BoostSplit.cpp )



find_package(gflags REQUIRED)
add_executable(For_Flags src/for_flag/main.cpp
                            src/for_flag/b.cpp
                            src/for_flag/my_flag.cpp
                            src/for_flag/config.cpp)


target_link_libraries(For_Flags gflags)



# 查找 Protobuf
find_package(Protobuf REQUIRED)

# 设置生成目录
set(GEN_PROTO_DIR ${CMAKE_BINARY_DIR}/gen/proto)
file(MAKE_DIRECTORY ${GEN_PROTO_DIR})

# 获取 .proto 文件的绝对路径
set(PROTO_FILE ${CMAKE_SOURCE_DIR}/src/proto/addressbook.proto)

# 获取文件名（不含路径和扩展名）
get_filename_component(PROTO_NAME ${PROTO_FILE} NAME_WE)

# 生成的文件
set(PROTO_SRC ${GEN_PROTO_DIR}/${PROTO_NAME}.pb.cc)
set(PROTO_HEADER ${GEN_PROTO_DIR}/${PROTO_NAME}.pb.h)

# 自定义命令：调用 protoc 生成 C++ 文件
add_custom_command(
        OUTPUT ${PROTO_SRC} ${PROTO_HEADER}
        COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
        --proto_path=${CMAKE_SOURCE_DIR}/src/proto  # 指定 import 搜索路径
        --cpp_out=${GEN_PROTO_DIR}
        ${PROTO_FILE}
        DEPENDS ${PROTO_FILE}
        COMMENT "Generating C++ proto files from ${PROTO_NAME}.proto"
        VERBATIM
)

# 创建一个对象库来封装生成的文件
add_library(proto_generated_obj OBJECT ${PROTO_SRC})
# 这样可以避免重复生成

# 创建可执行文件
add_executable(my_proto_example
        src/proto/my_proto_example.cpp
        $<TARGET_OBJECTS:proto_generated_obj>  # 使用生成的文件
)

# 添加生成头文件的目录到 include 路径
target_include_directories(my_proto_example PRIVATE ${GEN_PROTO_DIR})
target_include_directories(proto_generated_obj PRIVATE ${GEN_PROTO_DIR})

# 链接 Protobuf 库
target_link_libraries(my_proto_example PRIVATE ${PROTOBUF_LIBRARIES})

# 可选：打印调试信息
message(STATUS "Protobuf version: ${Protobuf_VERSION}")
message(STATUS "Protoc: ${PROTOBUF_PROTOC_EXECUTABLE}")
message(STATUS "Generated files in: ${GEN_PROTO_DIR}")






